﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using AuthenticationServiceUI.AuthenticateServiceReference;

namespace AuthenticationServiceUI
{
    public partial class Page : UserControl
    {
        private DispatcherTimer _serviceTimer = new DispatcherTimer();
        private AuthenticateServiceReference.AuthenticationServiceClient client = new AuthenticationServiceUI.AuthenticateServiceReference.AuthenticationServiceClient();
        private int _tickCounter = 0;

        public Page()
        {
            InitializeComponent();
            _serviceTimer.Tick += new EventHandler(_serviceTimer_Tick);
            _serviceTimer.Interval = new TimeSpan(0, 0, 1);
            _serviceTimer.Start();
            client.GetOnlineUsersCompleted += new EventHandler<AuthenticationServiceUI.AuthenticateServiceReference.GetOnlineUsersCompletedEventArgs>(client_GetOnlineUsersCompleted);
            client.GetSecurityTokenCollectionCompleted += new EventHandler<AuthenticationServiceUI.AuthenticateServiceReference.GetSecurityTokenCollectionCompletedEventArgs>(client_GetSecurityTokenCollectionCompleted);
            client.GetTokenCompleted += new EventHandler<AuthenticationServiceUI.AuthenticateServiceReference.GetTokenCompletedEventArgs>(client_GetTokenCompleted);
            client.ValidateTokenCompleted += new EventHandler<AuthenticationServiceUI.AuthenticateServiceReference.ValidateTokenCompletedEventArgs>(client_ValidateTokenCompleted);
        }

        /// <summary>
        /// Handles the GetOnlineUsersCompleted event of the client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="AuthenticationServiceUI.AuthenticateServiceReference.GetOnlineUsersCompletedEventArgs"/> instance containing the event data.</param>
        private void client_GetOnlineUsersCompleted(object sender, AuthenticationServiceUI.AuthenticateServiceReference.GetOnlineUsersCompletedEventArgs e)
        {
            onlineUsersGrid.ItemsSource = e.Result;
            OnlineUsersCount.Text = e.Result != null ? e.Result.Count().ToString() : "0";
            LastUpdatedOnlineUserValueLabel.Text = DateTime.Now.ToLocalTime().ToLongTimeString();
        }

        /// <summary>
        /// Handles the GetSecurityTokenCollectionCompleted event of the client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="AuthenticationServiceUI.AuthenticateServiceReference.GetSecurityTokenCollectionCompletedEventArgs"/> instance containing the event data.</param>
        private void client_GetSecurityTokenCollectionCompleted(object sender, AuthenticationServiceUI.AuthenticateServiceReference.GetSecurityTokenCollectionCompletedEventArgs e)
        {
            securityTokensGrid.ItemsSource = e.Result;
            SecurityTokensCount.Text = e.Result != null ? e.Result.Count().ToString() : "0";

            LastUpdatedSecurityTokenValueLabel.Text = DateTime.Now.ToLocalTime().ToLongTimeString();
        }

        /// <summary>
        /// Handles the Tick event of the _timer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void _serviceTimer_Tick(object sender, EventArgs e)
        {
            _tickCounter++;
            CurrentTimeValueLabel.Text = DateTime.Now.ToLocalTime().ToLongTimeString();
            if (_tickCounter > 5)
            {
                client.GetOnlineUsersAsync();
                client.GetSecurityTokenCollectionAsync();
                _tickCounter = 0;
            }
        }

        /// <summary>
        /// Handles the Click event of the LoginButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            LoginRequest loginRequest = new LoginRequest();
            loginRequest.User = new User
            {
                LoginId = LoginIdTextBox.Text,
                Password = PasswordTextBox.Text
            };
            client.LoginAsync(loginRequest);
        }

        /// <summary>
        /// Handles the Click event of the GetTokenButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void GetTokenButton_Click(object sender, RoutedEventArgs e)
        {
            TokenRequest tokenRequest = new TokenRequest();
            tokenRequest.User = new User
            {
                LoginId = GetTokenLoginIdTextBox.Text
            };
            client.GetTokenAsync(tokenRequest);
        }

        /// <summary>
        /// Handles the GetTokenCompleted event of the client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="AuthenticationServiceUI.AuthenticateServiceReference.GetTokenCompletedEventArgs"/> instance containing the event data.</param>
        void client_GetTokenCompleted(object sender, AuthenticationServiceUI.AuthenticateServiceReference.GetTokenCompletedEventArgs e)
        {
            if (e.Result.Fault == null)
            {
                GetTokenTextBox.Text = e.Result.Token.Id;
            }
        }

        /// <summary>
        /// Handles the Click event of the ValidateTokenButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void ValidateTokenButton_Click(object sender, RoutedEventArgs e)
        {
            TokenRequest tokenRequest = new TokenRequest();
            tokenRequest.Token = new Token
            {
                Id = ValidateTokenTextBox.Text
            };
            client.ValidateTokenAsync(tokenRequest);
        }

        /// <summary>
        /// Handles the ValidateTokenCompleted event of the client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="AuthenticationServiceUI.AuthenticateServiceReference.ValidateTokenCompletedEventArgs"/> instance containing the event data.</param>
        void client_ValidateTokenCompleted(object sender, AuthenticationServiceUI.AuthenticateServiceReference.ValidateTokenCompletedEventArgs e)
        {
            
        }

        /// <summary>
        /// Handles the Click event of the LogOutButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void LogOutButton_Click(object sender, RoutedEventArgs e)
        {
            client.SingOutAsync(LogoutTextBox.Text);
        }
    }
}
